<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
//use yii\grid\GridView;
use kartik\grid\GridView;
use backend\models\Country;
use backend\assets\KartikAsset;

//use kartik\file\FileInput;
use yii\bootstrap\ActiveForm;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\ProjectEaseSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '项目列表';
$this->params['breadcrumbs'][] = ['label' => '全部项目', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

KartikAsset::register($this);

$js = <<<JS
$(document).ready(function () {
    $("#project_file").fileinput({
            browseLabel:'选择文件',
            language:'zh',
            showBrowse:false,
            showCaption: false,
            dropZoneTitle:'点击或拖拽文件到这里',
            dropZoneClickTitle:'',           
            allowedFileExtensions: ["xls", "xlsx"],
            previewFileIconSettings: {
                'xls': '<i class="fa fa-file-excel-o text-success"></i>',              
            },
            previewFileExtSettings: {              
                'xls': function(ext) {
                    return ext.match(/(xls|xlsx)$/i);
                },              
            },
            browseOnZoneClick: true,
            uploadAsync: true,
            showUpload: true, 
            showRemove: true, 
            showUploadedThumbs: true,
            uploadUrl: $("#import-url").val(),
            deleteUrl: "",

        }).on('fileloaded', function(event, file, previewId, index, reader) {

        }).on('filebatchselected', function(event, files) {
            // console.log(files);
        })
    
  $('[data-toggle="tooltip"]').tooltip()
})
JS;

$this->registerJs($js);
echo '<input type="hidden" id="import-url" value="' . Url::to(['project-ease/import']) . '">';

?>

<div class="project-ease-index">
    <?php // echo $this->render('_search', ['model' => $searchModel]);

    $columns = [
        ['class' => 'yii\grid\SerialColumn'],
        [
            'attribute' => 'package_id',
            'value' => function ($model) {
                return Html::a($model->package->title, ['package-ease/update', 'id' => $model->package_id], ['target' => '_blank']);
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => ArrayHelper::map(\backend\models\PackageEase::find()->select(['id', 'title'])->asArray()->all(), 'id', 'title'),
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '隶属资产包'],
            'format' => 'raw',
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'level',
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'value' => function ($model) {
                return Yii::$app->params['project_ease_level'][$model->level];
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => Yii::$app->params['project_ease_level'],
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '等级'],
            'format' => 'raw',
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_WIDGET,
                'widgetClass' => \kartik\editable\Editable::INPUT_SELECT2,
                'options' => [
                    'data' => Yii::$app->params['project_ease_level'],
                ],
            ],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'borrower',
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXT,
            ],
        ],
        [
            'attribute' => 'province',
            'vAlign' => 'middle',
            'hAlign' => 'middle',
            'value' => function ($model) {
                return Country::findOne($model->province)->title;
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => Country::localCity('`pid`=0 AND `order`>0'),
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '省份'],
            'format' => 'raw',
        ],
        [
            'attribute' => 'city',
            'vAlign' => 'middle',
            'value' => function ($model) {
                if ($model->city) {
                    $modelCountry = new Country();
                    $city = implode(',', $modelCountry->getProvinces("id in ($model->city)"));
                } else {
                    $city = '';
                }
                return $city;
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => $searchModel->province ? Country::localCity("`pid`=" . $searchModel->province) : [],
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '城市'],
            'format' => 'raw'

        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'principal',
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . Yii::$app->format->formatCurrency($model->principal) . "' data-toggle='tooltip' data-placement='top'>" . Yii::$app->formatter->asDecimal($model->principal) . "</span>";
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXT,
            ],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'interest',
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . Yii::$app->format->formatCurrency($model->interest) . "' data-toggle='tooltip' data-placement='top'>" . Yii::$app->formatter->asDecimal($model->interest) . "</span>";
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXT,
            ],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'stakeholders',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->stakeholders . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->stakeholders, 0, 30) . "</span>";
            },
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXT,
            ],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'category',
            'value' => function ($model) {
                return Yii::$app->params['project_category'][$model->category];
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => Yii::$app->params['project_category'],
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '担保方式'],
            'format' => 'raw',
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_WIDGET,
                'widgetClass' => \kartik\editable\Editable::INPUT_SELECT2,
                'options' => [
                    'data' => Yii::$app->params['project_category'],
                ],
            ],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'is_suit',
            'value' => function ($model) {
                return Yii::$app->params['is_suit'][$model->is_suit];
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => Yii::$app->params['is_suit'],
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '是否诉讼'],
            'format' => 'raw',
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_WIDGET,
                'widgetClass' => \kartik\editable\Editable::INPUT_SELECT2,
                'options' => [
                    'data' => Yii::$app->params['is_suit'],
                ],
            ],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'suit',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->suit . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->suit, 0, 30) . "</span>";
            },
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXTAREA,
            ],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'execution',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->execution . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->execution, 0, 30) . "</span>";
            },
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXTAREA,
            ],
        ],
        [
            'attribute' => 'tag',
            'format' => 'raw',
            'value' => function ($model) {
                $arr_tag = explode(',', $model->tag);
                $arr_tag_tag = [];
                if (is_array($arr_tag) && count($arr_tag) > 0) {
                    foreach ($arr_tag as $item) {
                        $arr_tag_tag[] = \backend\models\Tags::findOne(['id' => $item])->title;
                    }
//                        return "<span title='" . implode(',', $arr_tag_tag) . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut(implode(',', $arr_tag_tag), 0, 30) . "</span>";
                    return implode(',', $arr_tag_tag);
                } else {
                    return;
                }
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => \backend\models\Tags::getAllTags(),
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true, 'multiple' => false],
            ],
            'filterInputOptions' => ['placeholder' => '选择标签'],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'source',
            'value' => function ($model) {
                return Yii::$app->params['project_ease_source'][$model->source];
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => Yii::$app->params['project_ease_source'],
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '来源'],
            'format' => 'raw',
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_WIDGET,
                'widgetClass' => \kartik\editable\Editable::INPUT_SELECT2,
                'options' => [
                    'data' => Yii::$app->params['project_ease_source'],
                ],
            ],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'detail',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->detail . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->detail, 0, 30) . "</span>";
            },
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXTAREA,
            ],

        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'highlights',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->highlights . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->highlights, 0, 30) . "</span>";
            },
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXTAREA,
            ],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'note',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->note . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->note, 0, 30) . "</span>";
            },
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXTAREA,
            ],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'follow',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->follow . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->follow, 0, 30) . "</span>";
            },
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXTAREA,
            ],
        ],
        [
            'attribute' => 'owner',
            'value' => function ($model) {
                return $model->publisher->realname;
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => \backend\models\User::getActiveUsers(),
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '上传人'],
            'format' => 'raw',
        ],
        [
            'attribute' => 'updated_at',
            'value' => function ($model) {
                if ($model->updated_at > 0) {
                    return date('Y-m-d', strtotime($model->updated_at));
                } else {
                    return '';
                }
            }
        ],
        ['class' => 'yii\grid\ActionColumn', 'template' => '{update} {delete}',
            'buttons' => [
                'delete' => function ($url, $model, $key) {
                    return $model->package->owner == Yii::$app->user->id ? Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
                        'data-confirm' => '确定要删除吗？',
                        'data-method' => 'post',
                    ]) : '';
                },
            ],
        ],
    ];
    echo $this->render('_column', ['model' => $searchModel, 'columns' => $columns]);
    if ($searchModel->columns) {
        $new_columns[] = ['class' => 'yii\grid\SerialColumn'];
        foreach ($columns as $column) {
            if (in_array($column['attribute'], $searchModel->columns)) {
                $new_columns[] = $column;
            }
        }
        $new_columns[] = ['class' => 'yii\grid\ActionColumn', 'template' => '{update} {delete}',
            'buttons' => [
                'delete' => function ($url, $model, $key) {
                    return $model->package->owner == Yii::$app->user->id ? Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
                        'data-confirm' => '确定要删除吗？',
                        'data-method' => 'post',
                    ]) : '';
                },
            ],
        ];
        $columns = $new_columns;
    }
    ?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'condensed' => true,
        'hover' => true,
        'responsive' => false,
//        'perfectScrollbar' => true,
        'toolbar' => [
            [
                'content' =>
                    Html::a('<i class="glyphicon glyphicon-plus"></i>创建', ['create'], ['title' => '创建项目', 'class' => 'btn btn-success']) .
                    Html::a('<i class="glyphicon glyphicon-repeat"></i>重置', ['index'], ['class' => 'btn btn-default', 'title' => '重置列表']) .
                    Html::button('导入', ['class' => 'btn btn-info', 'title' => '导入项目', 'data-toggle' => 'modal', 'data-target' => '#importExcelModal']) .
                    Html::a('<i class="glyphicon glyphicon-file"></i>模板', ['download-template'], ['class' => 'btn btn-warning', 'title' => '下载导入模板'])
            ],
//            '{export}',
            '{toggleData}'
        ],
        'panel' => [
            'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-book"></i>项目</h3>',
            'type' => 'success',
//            'before' => Html::a('<i class="glyphicon glyphicon-plus"></i> 创建资产包', ['create'], ['class' => 'btn btn-success']) . '&nbsp;&nbsp;' . Html::a('<i class="glyphicon glyphicon-import"></i> 上传资产包', ['import'], ['class' => 'btn btn-success']) . '&nbsp;&nbsp;' . Html::a('<i class="glyphicon glyphicon-file"></i> 上传模板', ['create'], ['class' => 'btn btn-success']),
//            'after' => Html::a('<i class="glyphicon glyphicon-repeat"></i>重置列表', ['index'], ['class' => 'btn btn-info']),
//            'footer' => '导出{export}',
            'footer' => '',
        ],
        'columns' => $columns,
    ]);
    ?>
</div>

<!-- Modal -->
<div class="modal fade" id="importExcelModal" tabindex="-1" role="dialog" aria-labelledby="importExcelModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="importExcelModalLabel">导入项目</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <?php ActiveForm::begin(['action' => ['/project-ease/import'], 'method' => 'post', 'options' => ['enctype' => "multipart/form-data"]]) ?>
            <div class="modal-body">
                <div class="file-loading">
                    <input id="project_file" name="project_file" type="file"'>
                </div>
                <div id="kartik-file-errors"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal"
                        onclick="javascript:window.location.reload();">关闭
                </button>
                <!--                <button type="submit" class="btn btn-primary">提交</button>-->
            </div>
            <?php ActiveForm::end() ?>
        </div>
    </div>
</div>

